package com.jarvis.pzz;

import android.Manifest;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;

import com.jarvis.bottomnavigationviewlibrary.BottomNavigationItem;
import com.jarvis.bottomnavigationviewlibrary.BottomNavigationView;
import com.jarvis.bottomnavigationviewlibrary.OnBottomNavigationItemClickListener;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.modules.main.MainFragment;
import com.jarvis.pzz.modules.my.MyFragment;
import com.jarvis.pzz.modules.requirement.RequirementFragment;
import com.jarvis.pzz.modules.shop.ShopFragment;
import com.jarvis.pzz.util.UpdateManager;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import rx.functions.Action1;

public class MainActivity extends BaseActivity implements OnBottomNavigationItemClickListener {
    @BindView(R.id.bottomNavigation)
    BottomNavigationView bottomNavigationViews;
    private MainFragment mainFragmet;
    private ShopFragment shopFragment;
    private RequirementFragment requirementFragment;
    private DemoFragment demoFragment;
    private MyFragment myFragment;
    public static MainActivity Instance;
    private long exitTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        MobclickAgent.openActivityDurationTrack(false);
        UpdateManager manager = new UpdateManager(this);
        manager.checkUpdate();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.openActivityDurationTrack(false);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void initView() {
        Instance = this;
        BottomNavigationItem item_main = new BottomNavigationItem("首页", getResources().getColor(R.color.purple), R.drawable.img_home_press, R.drawable.img_home);
        BottomNavigationItem item_shop = new BottomNavigationItem("查看商铺", getResources().getColor(R.color.purple), R.drawable.img_view_shop_press, R.drawable.img_view_shop);
        BottomNavigationItem item_requirement = new BottomNavigationItem("发布需求", getResources().getColor(R.color.purple), R.drawable.img_release_press, R.drawable.img_release);
        BottomNavigationItem item_my = new BottomNavigationItem("我", getResources().getColor(R.color.purple), R.drawable.img_mine_press, R.drawable.img_mine);
//        BottomNavigationItem item_demo = new BottomNavigationItem("demo", getResources().getColor(R.color.purple), R.drawable.img_report, R.drawable.img_report_gray);
        bottomNavigationViews.addTab(item_main);
        bottomNavigationViews.addTab(item_shop);
        bottomNavigationViews.addTab(item_requirement);
        bottomNavigationViews.addTab(item_my);
//        bottomNavigationViews.addTab(item_demo);
        bottomNavigationViews.setItemActiveColorWithoutColoredBackground(getResources().getColor(R.color.purple));
        bottomNavigationViews.isColoredBackground(false);
        bottomNavigationViews.setOnBottomNavigationItemClickListener(this);
        bottomNavigationViews.selectTab(0);
        getPermission();
    }

    @Override
    public void onNavigationItemClick(int index) {
        switch (index) {
            case 0:
                if (null == mainFragmet) {
                    mainFragmet = MainFragment.newInstance();
                }
                changeFragment(R.id.main_frame, mainFragmet);
                break;
            case 1:
                if (null == shopFragment) {
                    shopFragment = ShopFragment.newInstance();
                }
                changeFragment(R.id.main_frame, shopFragment);
                break;

            case 2:
                if (null == requirementFragment) {
                    requirementFragment = RequirementFragment.newInstance();
                }
                changeFragment(R.id.main_frame, requirementFragment);
                break;
            case 3:
                if (null == myFragment) {
                    myFragment = MyFragment.newInstance();
                }
                changeFragment(R.id.main_frame, myFragment);
                break;
        }
    }


    public void toShop() {
        bottomNavigationViews.selectTab(1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void getPermission() {
        RxPermissions.getInstance(this)
                .request(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                )
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) { // 在android 6.0之前会默认返回true
                            // 已经获取权限
                        } else {
                            // 未获取权限
                            showTextToast("请选择允许使用权限");
                        }
                    }
                });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {

            if((System.currentTimeMillis()-exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }
            else
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
